package atom;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-10-24
 * Time: 20:11
 */

public class Demo2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //通过Callable描述一个任务
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 0; i <= 1000; i++) {
                    sum += i;
                }
                return sum;
            }
        };
        //为了让线程执行执行callable中的任务 光一个构造方法还不够 还需要一个辅助的类
        FutureTask<Integer> task = new FutureTask<>(callable);
        //创建线程，来完成这里计算的工作
        Thread t = new Thread(task);
        t.start();

        System.out.println(task.get());
    }
}
